package aom.ju.ss.view.user.shop.management.goods

import android.text.Editable
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentPagerAdapter
import androidx.viewpager.widget.ViewPager
import aom.ju.ss.R
import aom.ju.ss.databinding.GoodsManageBinding
import aom.ju.ss.utils.TextWatcherLP
import aom.ju.ss.utils.start
import aom.ju.ss.view.base.BaseActivity
import aom.ju.ss.view.user.shop.management.goods.addGoods.NewCreateGoodsActivity

/**
 * 商品管理
 */
class GoodsManage : BaseActivity() {

    private lateinit var binding: GoodsManageBinding
    private var fs = arrayListOf<GoodsManageListFragment>()
    private var ts = arrayListOf("在售中", "已售罄", "已下架", "草稿箱")

    companion object {
        var ss = ""
    }

    var viewPagersw = false
    override fun init() {

        binding = viewBinding as GoodsManageBinding
        binding.title.r_title.text = "添加"
        binding.title.r_title.setOnClickListener {
            start(NewCreateGoodsActivity::class.java)
        }

        fs.add(GoodsManageListFragment.getMys("1"))
        fs.add(GoodsManageListFragment.getMys("2"))
        fs.add(GoodsManageListFragment.getMys("3"))
        fs.add(GoodsManageListFragment.getMys("4"))
        binding.viewPager.adapter = object : FragmentPagerAdapter(supportFragmentManager, 1) {
            override fun getCount(): Int {
                return fs.size

            }

            override fun getPageTitle(position: Int): CharSequence? {
                return ts[position]
            }

            override fun getItem(position: Int): Fragment {
                return fs[position]
            }

        }
        binding.tab.setupWithViewPager(binding.viewPager)
        binding.viewPager.offscreenPageLimit = 4
        binding.viewPager.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
            override fun onPageScrolled(
                position: Int,
                positionOffset: Float,
                positionOffsetPixels: Int
            ) {
            }

            override fun onPageSelected(position: Int) {

                viewPagersw = true
                binding.ss.setText("")

            }

            override fun onPageScrollStateChanged(state: Int) {
            }

        })

        binding.ss.addTextChangedListener(object : TextWatcherLP() {
            override fun afterTextChanged(s: Editable?) {
                super.afterTextChanged(s)
                if (!viewPagersw) {
                    ss = binding.ss.text.toString()
                    fs[binding.viewPager.currentItem].ok.ClearAll()
                    fs[binding.viewPager.currentItem].page = 1
                    fs[binding.viewPager.currentItem].data
                }
                viewPagersw = false
            }
        })

    }

    override val lay: Int = R.layout.goods_manage


}